﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UM_SERVER.Packet.InPacket
{
    /// <summary>
    ///  *   获取分组信息包
    ///  *   基本包数据结构
    ///  *   用户名 : String
    ///  *   用户聊天端口 : int
    ///  *   用户IP地址 : String
    /// </summary>
    class GetGroupPacket : BaseInPackage
    {
        public GetGroupPacket(InPackage inpackage)
            : base(inpackage)
        {
        }

        public override void InitPackage()
        {
            int Offset = 0;
            //用户名长度
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //用户名长度
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length + 2;
            //IP地址长度
            byte[] IPLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPLength, 0, 4);
            Offset += 4;
            //IP地址
            byte[] IPAddress = new byte[BitConverter.ToInt32(IPLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPAddress, 0, IPAddress.Length);
            Offset += IPAddress.Length;
            //聊天端口
            byte[] Talkport = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Talkport, 0, 4);

            this.DIC_CONTENT.Add("USERNAME",Username);
            this.DIC_CONTENT.Add("IPADDRESS",IPAddress);
            this.DIC_CONTENT.Add("TALKPORT",Talkport);
        }

        /// <summary>
        ///     得到用户IP
        /// </summary>
        /// <returns>
        ///     返回用户IP
        /// </returns>
        public String GetUserIpAddress()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["IPADDRESS"]);
        }
        /// <summary>
        ///     得到聊天端口
        /// </summary>
        /// <returns>
        ///     返回聊天端口
        /// </returns>
        public int GetTalkPort()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["TALKPORT"], 0);
        }
    }
}
